import psycopg

with psycopg.connect("dbname=examdb user=examdb") as conn: # 建立连接
    with conn.cursor() as cur: # 使用游标
        cur.execute("""
        SELECT s.name, c.name, g.grade 
        FROM course_grade as g
            LEFT OUTER JOIN student as s ON g.stu_sn = s.sn
            LEFT OUTER JOIN course as c  ON g.cou_sn = c.sn ;
        """)

        for row in cur: # 从游标中逐条迭代记录
            print(f"{row[0]}, {row[1]}, {row[2]}")

